package com.jnu.book;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;
import android.widget.Toast;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

public class BookListMainActivity extends AppCompatActivity {

    private String[] titles={"图书", "新闻","卖家"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ViewPager2 viewPager2=findViewById(R.id.viewpage2);
        viewPager2.setAdapter(new MyFragmentAdapter(this));

        TabLayout tabLayoutHeader=findViewById(R.id.tablayout_header);
        TabLayoutMediator tabLayoutMediator=new TabLayoutMediator(tabLayoutHeader, viewPager2, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                tab.setText(titles[position]);


            }
        });
        tabLayoutMediator.attach();
    }


    private class MyFragmentAdapter extends FragmentStateAdapter {


        public MyFragmentAdapter(@NonNull FragmentActivity fragmentActivity) {
            super(fragmentActivity);
        }

        @NonNull
        @Override
        public Fragment createFragment(int position){
            switch (position)
            {
                case 0:
                    return BookListFragment.newInstance();
                case 1:
                    return WebViewFragment.newInstance();
                default:
                    return MapFragment.newInstance();

            }
        }
        @Override
        public int getItemCount(){
            return 3;
        }
    }
}